package cn.yunyichina.provider.hisiface.invoke.openservice;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.yunyichina.provider.framework.dubbo.service.PayService;
import cn.yunyichina.provider.hisiface.invoke.IOutsideInvoke;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 原渠道退费
 * 
 * @author zhou
 * @date 2017年7月20日
 *
 */
public class RefundGeneralService implements IOutsideInvoke {
	
	static Logger logger = LoggerFactory.getLogger(RefundGeneralService.class);

	@Override
	public String execute(Map<String, String> params) {
		String paramsStr = JSON.toJSONString(params);
		logger.info("openService:refundGeneral 接收到前置机的入参: {}", paramsStr);
		
		Map<String, String> toPlatform = new HashMap<>();
		toPlatform.put("order_no", params.get("yyt_ord_num"));
		toPlatform.put("his_order_no", params.get("his_new_ord_num"));
		toPlatform.put("his_serial_no", params.get("his_bill_ord_num"));
		toPlatform.put("refund_amount", params.get("refund_amount"));
        toPlatform.put("refund_time", params.get("refund_time"));
        toPlatform.put("refund_reason", params.get("refund_reason"));
        toPlatform.put("refund_flag", params.get("partial_or_all_refund"));
        toPlatform.put("push_type", params.get("push_type"));
        
        String method = "yyt.pay.refund.channel.origin";
        String toPlatformJson = JSON.toJSONString(toPlatform);
        logger.info("openService:refundGeneral 请求[{}]入参: {}", method, toPlatformJson);
        Response response = PayService.callHumpFromInternal(method, toPlatformJson);
        logger.info("openService:refundGeneral 接收到[{}]的返回: {}", method, JSON.toJSON(response));
        
        Response toFront = null;
        if ("0".equals(response.getResultCode()) || "3010".equals(response.getResultCode())) {
        	// "3010"表示全额医保退费不调用海鹚接口
        	JSONObject dataJson = JSON.parseObject(response.getResult());
        	Map<String, String> data = new HashMap<>();
        	data.put("yyt_new_ord_num", dataJson.getString("refund_yyt_deal_id"));
        	data.put("agt_ref_ord_num", dataJson.getString("payment_deal_id"));
        	toFront = new Response("0", "原渠道退款成功!", JSON.toJSONString(data));
        } else {
        	toFront = new Response("-1", response.getResultMessage());
        }
        
        String result = JsonUtils.toJsonString(toFront);
		logger.info("openService:refundGeneral 返回给前置机: {}", result);
		
		return result;
	}

}
